using System;
using System.Collections.Generic;
using System.Text;

namespace Technoera.Common.Nh
{
    public class NhManager
    {
        public NhManager(string connectionString, string driverClass, string provider, string dialect, System.Reflection.Assembly[] mappingsAssemblies)
        {
            hibernateConnectionConnectionString = connectionString;
            hibernateConnectionDriverClass = driverClass;
            hibernateConnectionProvider = provider;
            hibernateDialect = dialect;
            hibernateMappingAssemblies = mappingsAssemblies;
        }

        public NhManager(string connectionString, string driverClass, string provider, string dialect, System.Reflection.Assembly[] mappingsAssemblies, string isolation, string defaultSchema)
            :this(connectionString, driverClass, provider, dialect, mappingsAssemblies)
        {
            hibernateConnectionIsolation = isolation;
            hibernateDefault_schema = defaultSchema;
        }

        public NhManager(string connectionString, string driverClass, string provider, string dialect, System.Reflection.Assembly mappingsAssembly) 
            : this(connectionString, driverClass, provider, dialect, new System.Reflection.Assembly[] { mappingsAssembly }) { }

        private string hibernateConnectionProvider = string.Empty;
        private string hibernateDialect = string.Empty;
        private string hibernateConnectionDriverClass = string.Empty;
        private string hibernateConnectionConnectionString = string.Empty;
        private System.Reflection.Assembly[] hibernateMappingAssemblies;
        private string hibernateConnectionIsolation = string.Empty;
        private string hibernateDefault_schema = string.Empty;
        
        private NHibernate.ISessionFactory sessionFactory = null;
        private NHibernate.Cfg.Configuration configuration = null;

        public NHibernate.Cfg.Configuration Configuration
        {
            get
            {
                if (configuration == null)
                {
                    configuration = new NHibernate.Cfg.Configuration();
                    configuration.SetProperty("connection.provider", hibernateConnectionProvider);
                    configuration.SetProperty("dialect", hibernateDialect);
                    configuration.SetProperty("connection.driver_class", hibernateConnectionDriverClass);
                    configuration.SetProperty("connection.connection_string", hibernateConnectionConnectionString);
                    if (hibernateConnectionIsolation != string.Empty)
                    {
                        configuration.SetProperty("connection.isolation", hibernateConnectionIsolation);
                    }
                    if (hibernateDefault_schema != string.Empty)
                    {
                        configuration.SetProperty("default_schema", hibernateDefault_schema);
                    }

                    foreach (System.Reflection.Assembly assembly in hibernateMappingAssemblies)
                    {
                        configuration.AddAssembly(assembly);
                    }
                }

                return configuration;
            }
        }

        public NHibernate.ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                    sessionFactory = this.Configuration.BuildSessionFactory();
                    
                return sessionFactory;
            }
        }

        /// <summary>
        /// Returns a new NHibernate.ISession with FlushMode set to FlushMode.Never
        /// </summary>
        /// <returns></returns>
        public NHibernate.ISession GetNewSession()
        {
            NHibernate.ISession session = this.SessionFactory.OpenSession();
            session.FlushMode = NHibernate.FlushMode.Never;
            return session;
        }
    }
}
